package com.ysyc.driver_ee.adapter;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ysyc.driver_ee.R;
import com.ysyc.driver_ee.activity.DistributeOrderActivity;
import com.ysyc.driver_ee.activity.MainActivity;
import com.ysyc.driver_ee.activity.OrderDetailsActivity;
import com.ysyc.driver_ee.activity.VerificationCodeActivity;
import com.ysyc.driver_ee.api.HttpResource;
import com.ysyc.driver_ee.api.NetworkCallback;
import com.ysyc.driver_ee.api.RetrofitClient;
import com.ysyc.driver_ee.base.BaseAdapter;
import com.ysyc.driver_ee.model.OrderForm;
import com.ysyc.driver_ee.tools.DateUtils;
import com.ysyc.driver_ee.tools.StatusSettingUtils;
import com.ysyc.driver_ee.tools.ToastUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;
import retrofit2.http.PUT;

/**
 * <p>描述：(这里用一句话描述这个类的作用)</p>
 * 作者： Chan<br>
 * 日期： 2018/11/10 14:41<br>
 * 版本： v1.0<br>
 */
public class IncompleteOrdersAdapter extends BaseAdapter<OrderForm> {


    public TextView tvOrdersView;
    public Observable<Object> objectObservable;
    private NetworkCallback<Object> objectNetworkCallback;

    public IncompleteOrdersAdapter(Activity activity, int layoutView, int layoutType, int captainType) {
        super(activity, layoutView, layoutType, captainType);
    }

    @Override
    public void convert(MyViewHolder holder, OrderForm item) {
        View itemView = holder.itemView;

        TextView tvProjectView = itemView.findViewById(R.id.tv_project_view);
        TextView tvProjectExplanationView = itemView.findViewById(R.id.tv_project_explanation_view);
        TextView tvTimeServiceView = itemView.findViewById(R.id.tv_time_service_view);
        TextView tvStatusView = itemView.findViewById(R.id.tv_status_view);
        TextView tvStartingPointView = itemView.findViewById(R.id.tv_starting_point_view);
        TextView tvEndPointView = itemView.findViewById(R.id.tv_end_point_view);
        TextView tvRejectionView = itemView.findViewById(R.id.tv_rejection_view);
        LinearLayout rlOrderView = itemView.findViewById(R.id.rl_order_view);
        tvOrdersView = itemView.findViewById(R.id.tv_orders_view);
        RelativeLayout rlOrderDetailsView = itemView.findViewById(R.id.rl_order_details_view);

        itemView.setOnClickListener(v -> {
            Intent intent = new Intent(mActivity,OrderDetailsActivity.class);
            intent.putExtra("layoutType",layoutType);
            intent.putExtra("orderNumber",item.orderNumber);
            mActivity.startActivity(intent);

        });



//       司机
        if (0==layoutType){
            if (0==captainType){
                tvRejectionView.setVisibility(View.VISIBLE);
                rlOrderView.setVisibility(View.GONE);
            }else if (1==captainType){
                tvRejectionView.setVisibility(View.GONE);
                rlOrderView.setVisibility(View.VISIBLE);
            }
            tvOrdersView.setText("接单");
            tvOrdersView.setOnClickListener(v -> {
                objectObservable = RetrofitClient.getInstance().getHttpServices().takeOrderform(item.orderNumber);
                objectNetworkCallback.postRequest(objectObservable,null);
            });
        }else if (1==layoutType){
            tvRejectionView.setVisibility(View.GONE);
            rlOrderView.setVisibility(View.GONE);
            tvOrdersView.setText("接单");
            StatusSettingUtils.setOrderFromStatus(tvOrdersView,item.status);


        }
        tvProjectView.setText(item.projectName);
        tvTimeServiceView.setText(DateUtils.orderTime(item.startArriveTime));
        tvStatusView.setText("¥"+item.price/100);
        tvStartingPointView.setText(item.startName);
        tvEndPointView.setText(item.endName);

        rlOrderView.setOnClickListener(v -> {
            Intent intent = new Intent(mActivity, DistributeOrderActivity.class);
            intent.putExtra("orderNumber",item.orderNumber);
            mActivity.startActivity(intent);

        });
    }

    public void initNetworkRequest(Refresh refresh) {
        objectNetworkCallback = new NetworkCallback<>(new HttpResource<Object>() {
            @Override
            public void onSuccess(Object o) {
                ToastUtils.show("接单成功");
                refresh.onRefresh();
                ((MainActivity)mActivity).dismissDilog();
            }

            @Override
            public void onFailure(String errorDes) {
                ((MainActivity)mActivity).dismissDilog();
                ToastUtils.show(errorDes);
            }

            @Override
            public void onLoading(Dialog dialog) {
                ((MainActivity)mActivity).showLoadingDialog();
            }
        });

    }


    public interface Refresh{
        public void onRefresh();
    }


}
